/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) Yiu Ming Patrick Ma 2008 <fivetwentysix@gmail.com>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <strings.h>
#include "authentication.c"
#include "strip_newline.c"
int login ( );
void writestring ( );
int main( ) {
	int auth;
	auth = login();
	while ( auth == 1 ) {
		printf( "Welcome to 0001\nPlease enter a string of up to 255 chacters:\n" );
		writestring();
		getchar();
		return 0;
	}
	return 0;
}
int login ( ) {
/* Because I wanted to be able to write a reusable authentication method
	 I created a simple login procedure for the terminal to test it */
/* Below we create two character arrays to allow for strings of up to 16 
	 characters. We then as the user to enter his username, compare it with 
	 what we have in storage, if it has an identical match, we then let the user
	 enter his password to as well be authenticated. */
	char username[17], password [17];
	printf ( "Enter your username: ");
	fgets ( username , 16, stdin );
/* strip_newline( *string, size ) is a popular method of stripping the '\n' out of 
	the strings because hitting enter on fgets' stdin will create a '\n', having the
	ability to strip that character and replace it with '\0' will return the string to a 
	comparable form. */
	strip_newline ( username, 16 );
	if ( auth_username ( username ) == 1 ) {
		printf ( "Enter your password: ");
		fgets ( password, 16 , stdin );
		strip_newline( password, 16 );
		if ( auth_password ( password ) == 1 ) {
			printf ( "Authenticated" );
			return 1;
		}
	}
	else
		return 0;
}
void writestring ( ) {
	char string[256];
	FILE *fp;
	fp = fopen("output.txt", "a");
	fgets( string, 255, stdin );
	fprintf(fp, string);
	return 0;
}